/*
 * 函数名的书写规则：以动词开头，第一个单词小写，第一个单词以后的每个单词的第一个字母大写；
 * 如果是AJAX的回调函数，则函数名的写法为：发送AJAX请求的函数名加上 "_callback"后缀作为回调函数名
 * */

function checkUser(userName, passWord, validateCode)
{
	var strName = $F(userName);
	var strPwd = $F(passWord);
	var strCode = $F(validateCode);
	if (dream.String.isNullOrEmpty(strName))
	{
		alert("您还没有输入用户名，请先输入用户名");
		return false;
	}
	if (dream.String.isNullOrEmpty(strPwd))
	{
		alert("密码不能为空");
		return false;
	}
	if (dream.String.isNullOrEmpty(strCode))
	{
		alert("还没有输入验证码");
		return false;
	}
	
	//document.domain = "*.blue-dream.com";
	var url = "/ajaxapi/user/login.jsp";
	var pars = "username=" + strName + "&password=" + strPwd + "&validatecode=" + strCode;
	var request = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: checkUser_callback, onException: exception_callback});
}

function exception_callback(req, exception)
{
	alert("发生了异常 : \n" + exception.description);
}

function checkUser_callback(req)
{
	//alert("req.responseText : " + req.responseText);
	var retObj = eval("(" + req.responseText + ")");
	if (null != retObj)
	{
		/*closeLoginFrame();
		var loginButton = $("loginbutton");
		var imgNode = loginButton.getElementsByTagName("img")[0];
		imgNode.setAttribute("src", "/images/logoutbtn.gif");
		loginButton.setAttribute("id", "logoutbutton");
		loginButton.setAttribute("href", "javascript:logout(this)");
		
		var aNode = $("prompt_user").getElementsByTagName("a")[0];
		aNode.innerHTML = retObj["username"];
		aNode.setAttribute("href", "/root/myspace.jsp");*/
		initCurPage();
	}
	else
	{
		alert("用户名或密码错误，请重新输入");
	}
}

function openLoginFrame(me)
{
	var loginFront = $("loginlayer");
	loginFront.className = "loginLayerShow";
	var loginForm = $("loginform");
	loginForm.elements["username"].focus();
	loginForm.elements["password"].value = "";
	loginForm.elements["validatecode"].value = "";
	var imgObj = $("logon_validatecodeimg");
	var oldSrc = imgObj.getAttribute("src");
	imgObj.setAttribute("src", oldSrc.substring(0, oldSrc.lastIndexOf("&")) + "&rdn=" + Math.random()); 
	
	//调试用
	loginForm.elements["username"].value = "caifeifei";
	loginForm.elements["password"].value = "112997";
}

function closeLoginFrame()
{
	var loginFront = $("loginlayer");
	loginFront.className = "loginLayerHide";
}

function logout(me)
{
	var url = "/ajaxapi/user/logout.jsp";
	//var params = "type=logout";
	//var request = new Ajax.Request(url, {method: 'get', parameters: params, onComplete: logout_callback});
	var request = new Ajax.Request(url, {method: 'get', onComplete: logout_callback});
}

function logout_callback(req)
{
	//alert("req.responseText : " + req.responseText);
	if (true == eval(req.responseText))
	{
		var aNode = $("prompt_user").getElementsByTagName("a")[0];
		aNode.innerHTML = "访客";
		aNode.removeAttribute("href");
		
		var logoutButton = $("logoutbutton");
		logoutButton.innerHTML = "登陆";
		logoutButton.setAttribute("href", "javascript:openLoginFrame(this)");
		logoutButton.setAttribute("id", "loginbutton");
		var loginForm = $("loginform");
		loginForm.elements["username"].value = "";
		loginForm.elements["password"].value = "";
	}
}

function hasLogin(me)
{
	var url = "/ajaxapi/user/validate.jsp";
	//var params = "type=checklogin";
	//var request = new Ajax.Request(url, {method: 'get', parameters: params, onComplete: hasLogin_callback.bind(me)});;
	var request = new Ajax.Request(url, {method: 'get', onComplete: hasLogin_callback.bind(me)});;
}

function hasLogin_callback(req)
{
	//alert("req.responseText : " + req.responseText);
	if (true == eval(req.responseText))
	{
		window.location = this.getAttribute("href");
		return true;
	}
	alert("		您还没有登陆，请先登陆！\n\n" +
		"注意：该页面只对特殊用户开放，如果您是本班的成员之一（家长、\n" +
		"学生、科任老师之一，但还没有用户名和密码，请与本班班主任联系！\n" +
		"感谢您的支持～！");
	return false;
}

function sendMail(me, kindObj)
{
	var url = "/ajaxapi/mail/sendmail.jsp";
	var request = new Ajax.Request(url, 
		{method:'post', postBody:'mailtitle=' + $F(me["mailtitle"]) + '&mailto=' + $F(me["mailto"]) + '&mailcontent=' + kindObj.data(), onComplete: sendMail_callback.bind({"mailto": $F(me["mailto"])})});
}

function sendMail_callback(req)
{
	if (true == eval(req.responseText))
		alert("留言被成功发送给:" + this["mailto"]);
	else
		alert("由于未知错误，你的留言没有发送出去");
}